package com.quectel.core.module.banner.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.banner.dao.MobileBannerVillageDao;
import com.quectel.core.module.banner.dto.MobileBannerVillageDto;
import com.quectel.core.module.banner.entity.MobileBannerVillageEntity;
import com.quectel.core.module.banner.service.MobileBannerVillageService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-24 15:50:39
 */
@DubboService
public class MobileBannerVillageServiceImpl implements MobileBannerVillageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MobileBannerVillageServiceImpl.class);

    private static final String MOBILE_BANNER_VILLAGE_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "MOBILE_BANNER_VILLAGE_CACHE_BY_ID:";

    @Autowired
    private MobileBannerVillageDao mobileBannerVillageDao;
    @Autowired
    private VillageService villageService;


    /**
     * 通过 mobile banner id 查询绑定小区list
     *
     * @param mobileBannerId
     * @return
     */
    @Override
    public List<VillageDto> selectByMobileBannerId(Long mobileBannerId) {
        List<MobileBannerVillageEntity> entityList =
                mobileBannerVillageDao.selectList(new LambdaQueryWrapper<MobileBannerVillageEntity>()
                        .eq(MobileBannerVillageEntity::getMobileBannerId, mobileBannerId));
        return entityList.stream().map(v -> villageService.selectById(v.getVillageId()))
                .filter(v -> v != null).collect(Collectors.toList());
    }

    @Override
    public MobileBannerVillageDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                MOBILE_BANNER_VILLAGE_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public MobileBannerVillageDto selectById(Long id) {
        MobileBannerVillageEntity entity = mobileBannerVillageDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, MobileBannerVillageDto.class));
    }

    @Override
    public List<MobileBannerVillageDto> queryList(Map<String, Object> params) {
        List<MobileBannerVillageEntity> list = mobileBannerVillageDao.queryList(params);
        List<MobileBannerVillageDto> result = CopyUtils.copyList(list, MobileBannerVillageDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private MobileBannerVillageDto paddingField(MobileBannerVillageDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return mobileBannerVillageDao.queryTotal(params);
    }

    @Override
    public Long save(MobileBannerVillageDto dto) {
        MobileBannerVillageEntity entity = CopyUtils.copyObj(dto, MobileBannerVillageEntity.class);
        entity.setId(Snowflake.nextId());
        mobileBannerVillageDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(MobileBannerVillageDto dto) {
        MobileBannerVillageEntity entity = CopyUtils.copyObj(dto, MobileBannerVillageEntity.class);
        mobileBannerVillageDao.updateById(entity);
        CacheKit.invalidRedisCache(MOBILE_BANNER_VILLAGE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(MobileBannerVillageDto dto) {
        MobileBannerVillageEntity entity = CopyUtils.copyObj(dto, MobileBannerVillageEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, mobileBannerVillageDao);

        CacheKit.invalidRedisCache(MOBILE_BANNER_VILLAGE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        mobileBannerVillageDao.deleteById(id);

        CacheKit.invalidRedisCache(MOBILE_BANNER_VILLAGE_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
